package com.dronff.invoker;

import com.dronff.connection.TangConnection;
import com.dronff.handler.TangRPCInvokerChannelInitializer;
import com.dronff.listener.ConnectSuccessListener;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.apache.log4j.Logger;

import java.net.InetSocketAddress;

public class TangRPCInvokerBootstrap {
    static final Logger logger = Logger.getLogger(TangRPCInvokerBootstrap.class);
    public Bootstrap socket;
    public ChannelFuture connect;
    private int port;
    public void init(String...args){
        socket = new Bootstrap()
                .group(new NioEventLoopGroup(10))
                .channel(NioSocketChannel.class)
                .handler(
                        new TangRPCInvokerChannelInitializer()
                );
    }
    public TangConnection connect() throws InterruptedException {
        connect = socket.connect(
                new InetSocketAddress(
                        "localhost", 8666
                )
        );
        connect.addListener(
                new ConnectSuccessListener()
        );
        return TangConnection
                .builder()
                .ip("localhost")
                .port(8666)
                .channel(connect.channel())
                .build();
    }

}
